using System;
using System.Collections.Generic;
using Prime31;
using UnityEngine;

public class CustomAnalytics
{
	private static bool enableLogs = false;

	public static Vector2[] roundingValues_LivesSpentBetweenCheckpoints = new Vector2[3]
	{
		new Vector2(15f, 1f),
		new Vector2(50f, 5f),
		new Vector2(100f, 25f)
	};

	public static Vector2[] roundingValues_PointsEarnedByAttempts = new Vector2[3]
	{
		new Vector2(30f, 1f),
		new Vector2(100f, 5f),
		new Vector2(1000f, 25f)
	};

	public static Vector2[] roundingValues_LivesSpentToLevel = new Vector2[5]
	{
		new Vector2(50f, 1f),
		new Vector2(500f, 5f),
		new Vector2(5000f, 50f),
		new Vector2(50000f, 250f),
		new Vector2(100000f, 1000f)
	};

	public static Vector2[] roundingValues_LivesPurchasedToLevel = new Vector2[3]
	{
		new Vector2(1000f, 5f),
		new Vector2(10000f, 50f),
		new Vector2(50000f, 500f)
	};

	public static Vector2[] roundingValues_LivesRewardedToLevel = new Vector2[3]
	{
		new Vector2(500f, 5f),
		new Vector2(10000f, 50f),
		new Vector2(100000f, 500f)
	};

	public static Vector2[] roundingValues_WildcardsSpentToLevel = new Vector2[3]
	{
		new Vector2(100f, 1f),
		new Vector2(1000f, 5f),
		new Vector2(10000f, 50f)
	};

	public static Vector2[] roundingValues_TotalLives = new Vector2[3]
	{
		new Vector2(100f, 10f),
		new Vector2(500f, 50f),
		new Vector2(2500f, 250f)
	};

	public static Vector2[] roundingValues_LivesRewardedSpentRatio = new Vector2[2]
	{
		new Vector2(2f, 0.1f),
		new Vector2(4f, 0.5f)
	};

	public static Vector2[] roundingValues_LivesPurchases = new Vector2[2]
	{
		new Vector2(10f, 1f),
		new Vector2(80f, 5f)
	};

	public static Vector2[] roundingValues_PointsEarned = new Vector2[3]
	{
		new Vector2(30f, 5f),
		new Vector2(120f, 10f),
		new Vector2(300f, 20f)
	};

	public static Vector2[] roundingValues_ActualTimePassed = new Vector2[3]
	{
		new Vector2(10f, 1f),
		new Vector2(40f, 5f),
		new Vector2(120f, 10f)
	};

	public static Vector2[] roundingValues_recordAttempts = new Vector2[2]
	{
		new Vector2(150f, 1f),
		new Vector2(1000f, 25f)
	};

	public static Vector2[] roundingValues_AdByLivesAvailable = new Vector2[2]
	{
		new Vector2(100f, 1f),
		new Vector2(10000f, 25f)
	};

	public static Vector2[] roundingValues_WildcardsByWildcardsAvailable = new Vector2[2]
	{
		new Vector2(100f, 1f),
		new Vector2(1000f, 3f)
	};

	public static Vector2[] roundingValues_WildcardUseByLivesSpent = new Vector2[2]
	{
		new Vector2(200f, 1f),
		new Vector2(3000f, 10f)
	};

	public static Vector2[] roundingValues_FinishTime = new Vector2[2]
	{
		new Vector2(300f, 1f),
		new Vector2(1800f, 10f)
	};

	private static string apiKey = "N8HSJ6BK87YPC7SNQ7VG";

	public static string GetRoundedValues(int value, Vector2[] roundingValues, int stringLength = 6)
	{
		try
		{
			if (value != 0)
			{
				if (roundingValues == null || roundingValues.Length == 0)
				{
					return value.ToString("D" + stringLength);
				}
				int num = roundingValues.Length - 1;
				if ((float)value >= roundingValues[num].x)
				{
					return roundingValues[num].x.ToString("D" + stringLength);
				}
				if (roundingValues.Length == 1 || (float)value < roundingValues[0].x)
				{
					float num2 = value;
					num2 /= roundingValues[0].y;
					return (Mathf.RoundToInt(num2) * (int)roundingValues[0].y).ToString("D" + stringLength);
				}
				for (num = roundingValues.Length - 2; num >= 0; num--)
				{
					if ((float)value > roundingValues[num].x && (float)value < roundingValues[num + 1].x)
					{
						float num2 = (float)value - roundingValues[num + 1].x;
						num2 /= roundingValues[num + 1].y;
						return ((int)roundingValues[num + 1].x + Mathf.RoundToInt(num2) * (int)roundingValues[num + 1].y).ToString("D" + stringLength);
					}
				}
			}
			return value.ToString("D" + stringLength);
		}
		catch (Exception ex)
		{
			Debug.Log("GetRoundedValues failed for '" + value + "' : " + ex.Message);
			return string.Empty;
		}
	}

	public static string GetFloatRoundedValues(float value, Vector2[] roundingValues, int decimalPoints = 1)
	{
		try
		{
			if (value != 0f)
			{
				if (roundingValues == null || roundingValues.Length == 0)
				{
					return value.ToString("F" + decimalPoints);
				}
				int num = roundingValues.Length - 1;
				if (value >= roundingValues[num].x)
				{
					return roundingValues[num].x.ToString("F" + decimalPoints);
				}
				if (roundingValues.Length == 1 || value < roundingValues[0].x)
				{
					float f = value / roundingValues[0].y;
					return ((float)Mathf.RoundToInt(f) * roundingValues[0].y).ToString("F" + decimalPoints);
				}
				for (num = roundingValues.Length - 2; num >= 0; num--)
				{
					if (value > roundingValues[num].x && value < roundingValues[num + 1].x)
					{
						float f = value - roundingValues[num + 1].x;
						f /= roundingValues[num + 1].y;
						return (roundingValues[num + 1].x + (float)Mathf.RoundToInt(f) * roundingValues[num + 1].y).ToString("F" + decimalPoints);
					}
				}
			}
			return value.ToString("F" + decimalPoints);
		}
		catch (Exception ex)
		{
			Debug.Log(ex.Message);
			return string.Empty;
		}
	}

	public static void Init()
	{
		//FlurryAnalytics.startSession(apiKey);
	}

	public static void LogLivesSpentBetweenCheckpoints(int levelNumber, int fromCheckPointNumber, int toCheckPointNumber, int livesSpent)
	{
		Debug.Log("LogLivesSpentBetweenCheckpoints: levelNumber=" + levelNumber + " fromCheckPointNumber=" + fromCheckPointNumber + " toCheckPointNumber=" + toCheckPointNumber + " livesSpent=" + livesSpent);
		string eventName = "Level " + levelNumber.ToString("D3");
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		dictionary.Add("Lives Spent between Checkpoints", fromCheckPointNumber.ToString("D2") + "-" + toCheckPointNumber.ToString("D2") + ":" + GetRoundedValues(livesSpent, roundingValues_LivesSpentBetweenCheckpoints, 3));
		LogEvent(eventName, dictionary);
	}

	public static void LogLivesSpentCheckpointToExit(int levelNumber, int checkPointNumber, int livesSpent)
	{
		string eventName = "Level " + levelNumber.ToString("D3");
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		dictionary.Add("Lives Spent - Checkpoint to Exit", checkPointNumber.ToString("D2") + ":" + GetRoundedValues(livesSpent, roundingValues_LivesSpentBetweenCheckpoints, 4));
		LogEvent(eventName, dictionary);
	}

	public static void LogPointsEarnedByAttempts(int levelNumber, int points, int numberOfTries)
	{
		string eventName = "Level " + levelNumber.ToString("D3");
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		dictionary.Add("Points Earned by Attemps", points + ":" + GetRoundedValues(numberOfTries, roundingValues_PointsEarnedByAttempts, 4));
		LogEvent(eventName, dictionary);
	}

	public static void LogFirstLevelPlay(int levelNumber)
	{
		string eventName = "Level " + levelNumber.ToString("D3");
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		dictionary.Add("Lives Spent to Level", GetRoundedValues(Economy.totalLivesSpent, roundingValues_LivesSpentToLevel));
		dictionary.Add("Lives Purchased to Level", GetRoundedValues(Economy.totalLivesPurchased, roundingValues_LivesPurchasedToLevel));
		dictionary.Add("Lives Rewarded to Level", GetRoundedValues(Economy.totalLivesRewarded, roundingValues_LivesRewardedToLevel));
		dictionary.Add("Wildcards Spent to Level", GetRoundedValues(Economy.totalWildcardsSpent, roundingValues_WildcardsSpentToLevel));
		dictionary.Add("Wildcards Purchased to Level", GetRoundedValues(Economy.totalWildcardsPurchased, null));
		dictionary.Add("Wildcards Rewarded to Level", GetRoundedValues(Economy.totalWildcardsRewarded, null));
		LogEvent(eventName, dictionary);
	}

	public static void LogWildcardUse(int levelNumber)
	{
		string value = "Level " + levelNumber.ToString("D3");
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		dictionary.Add("Per level", value);
		dictionary.Add("By lives available", GetRoundedValues(Economy.lives, roundingValues_AdByLivesAvailable, 4));
		dictionary.Add("By lives spent", GetRoundedValues(Economy.livesSpentPerWildcard, roundingValues_WildcardUseByLivesSpent, 4));
		LogEvent("Wildcard Use", dictionary);
	}

	public static void LogWildcardUse(int levelNumber, int availableLives, int spentLives)
	{
		string value = "Level " + levelNumber.ToString("D3");
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		dictionary.Add("Per level", value);
		dictionary.Add("By lives available", GetRoundedValues(availableLives, roundingValues_AdByLivesAvailable, 4));
		dictionary.Add("By lives spent", GetRoundedValues(spentLives, roundingValues_WildcardUseByLivesSpent, 4));
		LogEvent("Wildcard Use", dictionary);
	}

	public static void LogRecordAttemptStarted(int levelNumber, int livesAvailable, int attemptsStarted)
	{
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		dictionary.Add("By Level", levelNumber.ToString("D3") + " - Started");
		dictionary.Add("Started by Lives Available", GetRoundedValues(livesAvailable, roundingValues_AdByLivesAvailable, 4));
		dictionary.Add("Count By Users", GetRoundedValues(attemptsStarted, roundingValues_recordAttempts, 3) + " - Started");
		LogEvent("Record", dictionary);
	}

	public static void LogRecordAttemptAchieved(int levelNumber, int atemptsCount, int attemptsAchieved)
	{
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		dictionary.Add("By Level", levelNumber.ToString("D3") + " - Achieved");
		dictionary.Add("Achieved by Attempts", atemptsCount.ToString());
		dictionary.Add("Count By Users", GetRoundedValues(attemptsAchieved, roundingValues_recordAttempts, 3) + " - Achieved");
		LogEvent("Record", dictionary);
	}

	public static void LogPlayTimeTrigger(string playTimeFormatted, int livesRewardedPerPlayTime, int livesSpentPerPlayTime, int totalLivesRewarded, int totalLivesSpent, int totalLivesPurchases, int totalWildcardsPurchases, bool removeAdsPurchased, bool unlockArenasPurchased, int points, int daysSinceFirstRun)
	{
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		float value = 100f;
		if (livesSpentPerPlayTime != 0)
		{
			value = (float)livesRewardedPerPlayTime / (float)livesSpentPerPlayTime;
		}
		dictionary.Add("Lives - Rewarded / Spent Ratio", playTimeFormatted + ":" + GetFloatRoundedValues(value, roundingValues_LivesRewardedSpentRatio));
		value = 100f;
		if (totalLivesSpent != 0)
		{
			value = (float)totalLivesRewarded / (float)totalLivesSpent;
		}
		dictionary.Add("Total Lives - Rewarded / Spent Ratio", playTimeFormatted + ":" + GetFloatRoundedValues(value, roundingValues_LivesRewardedSpentRatio));
		dictionary.Add("Total Lives Spent", playTimeFormatted + ":" + GetRoundedValues(totalLivesSpent, roundingValues_TotalLives, 4));
		dictionary.Add("Total Lives Rewarded", playTimeFormatted + ":" + GetRoundedValues(totalLivesRewarded, roundingValues_TotalLives, 4));
		dictionary.Add("Level Reached", playTimeFormatted + ":" + Economy.GetHighestFinishedLevel(true).ToString("000"));
		dictionary.Add("Lives Purchases", playTimeFormatted + ":" + GetRoundedValues(totalLivesPurchases, roundingValues_LivesPurchases, 2));
		dictionary.Add("Wildcards Purchases", playTimeFormatted + ":" + GetRoundedValues(totalWildcardsPurchases, roundingValues_LivesPurchases, 2));
		dictionary.Add("Points Earned", playTimeFormatted + ":" + GetRoundedValues(points, roundingValues_PointsEarned, 3));
		dictionary.Add("Actual Time Passed", playTimeFormatted + ":" + GetRoundedValues(daysSinceFirstRun, roundingValues_ActualTimePassed, 3));
		LogEvent("Play Time", dictionary);
	}

	public static void LogFinishTime(int levelNumber, float levelTimeInSeconds)
	{
		string eventName = "Level " + levelNumber.ToString("D3");
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		dictionary.Add("Finish Time", GetRoundedValues(Mathf.RoundToInt(levelTimeInSeconds), roundingValues_FinishTime, 4));
		LogEvent(eventName, dictionary);
	}

	public static void LogIAPByPlayTime(IAPManager.ProductType pType, string playTimeFormatted)
	{
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		string text = string.Empty;
		switch (pType)
		{
		case IAPManager.ProductType.LivesPack1:
			text = "spheres pack 1";
			break;
		case IAPManager.ProductType.LivesPack2:
			text = "spheres pack 2";
			break;
		case IAPManager.ProductType.LivesPack3:
			text = "spheres pack 3";
			break;
		case IAPManager.ProductType.WildcardsPack1:
			text = "wildcards pack 1";
			break;
		case IAPManager.ProductType.WildcardsPack2:
			text = "wildcards pack 2";
			break;
		case IAPManager.ProductType.WildcardsPack3:
			text = "wildcards pack 3";
			break;
		case IAPManager.ProductType.RemoveAds:
			text = "remove ads";
			break;
		case IAPManager.ProductType.Unlock:
			text = "unlock all";
			break;
		}
		dictionary.Add("Purchase by Play Time", playTimeFormatted + ":" + text);
		LogEvent("In-App Purchase", dictionary);
	}

	public static void LogNotificationsEvent(string parameterName, string parameterValue)
	{
		if (parameterName != null && parameterValue != null)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add(parameterName, parameterValue);
			LogEvent("Notifications", dictionary);
		}
	}

	public static void LogUIEvent(string parameterName, string parameterValue)
	{
		if (parameterName != null && parameterValue != null)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add(parameterName, parameterValue);
			LogEvent("UI", dictionary);
		}
	}

	private static void LogEvent(string eventName, Dictionary<string, string> dict)
	{
		if (enableLogs)
		{
			Debug.Log("Logging event " + eventName + " with params");
			Prime31.Utils.logObject(dict);
		}
		if (dict != null)
		{
			//FlurryAnalytics.logEvent(eventName, dict, false);
		}
		else
		{
			//FlurryAnalytics.logEvent(eventName, false);
		}
	}

	private static void StartTimedEvent(string eventName, Dictionary<string, string> dict)
	{
		if (enableLogs)
		{
			Debug.Log("Starting event " + eventName + " with params");
			Prime31.Utils.logObject(dict);
		}
		if (dict != null)
		{
			//FlurryAnalytics.logEvent(eventName, dict, true);
		}
		else
		{
			//FlurryAnalytics.logEvent(eventName, true);
		}
	}

	private static void EndTimedEvent(string eventName, Dictionary<string, string> dict)
	{
		if (enableLogs)
		{
			Debug.Log("Ending event " + eventName + " with params");
			Prime31.Utils.logObject(dict);
		}
		if (dict != null)
		{
			//FlurryAnalytics.endTimedEvent(eventName, dict);
		}
		else
		{
			//FlurryAnalytics.endTimedEvent(eventName);
		}
	}
}
